package com.yylc.cscore.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.yylc.cscore.bean.CustomerBean;
import com.yylc.cscore.bean.base.PageBean;
import com.yylc.cscore.dao.CustomerDao;
import com.yylc.cscore.entity.CustomerEntity;
import com.yylc.cscore.service.CustomerService;
import com.yylc.cscore.util.PageUtil;
import com.yylc.cscore.util.R;
import com.yylc.cscore.util.YYlcBeanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xiangnan
 * date 2018/4/27
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    private static final Logger logger = LogManager.getLogger();

    @Resource
    private CustomerDao customerDao;

    @Override
    public List<CustomerBean> getCustomerList() {
        List<CustomerBean> beanList = new ArrayList<>();
        List<CustomerEntity> entityList = customerDao.selectList();
        entityList.forEach(entity -> beanList.add(YYlcBeanUtils.copyProperties(entity, new CustomerBean())));

        return beanList;
    }

    @Override
    public List<CustomerBean> getCustomerList(String ServiceIds) {
        List<CustomerBean> beanList = new ArrayList<>();
        List<String> idList = CollectionUtil.newArrayList(StrUtil.split(ServiceIds, R.partFlag));
        idList.forEach(id -> {
            CustomerEntity entity = customerDao.select(id);
            beanList.add(entity != null ? YYlcBeanUtils.copyProperties(entity, new CustomerBean()) : new CustomerBean());
        });

        return beanList;
    }

    @Override
    public PageBean<CustomerBean> getCustomerList(int current, int pageSize) {
        int total = customerDao.selectPageListCount();
        if (!PageUtil.check(current, total, pageSize)) {
            throw new RuntimeException("页数错误");
        }

        List<CustomerEntity> entityList = customerDao.selectPageList(PageUtil.getStart(current, pageSize), pageSize);
        List<CustomerBean> beanList = new ArrayList<>();
        entityList.forEach(entity -> beanList.add(YYlcBeanUtils.copyProperties(entity, new CustomerBean())));

        return new PageBean<>(current, pageSize, total, beanList);
    }

    public int addCustomer(CustomerBean bean) {
        CustomerEntity entity = YYlcBeanUtils.copyProperties(bean, new CustomerEntity());
        entity.setStatus(R.customerStatus.INIT);

        logger.info("addCustomer: {}", entity);

        CustomerEntity oldEntity = customerDao.select(entity.getServiceId());
        if (oldEntity == null) {
            customerDao.insert(entity);
        } else {
            customerDao.update(entity);
        }
        logger.info("addCustomer {} success", bean.getServiceName());
        return 0;
    }

    public int updateCustomer(CustomerBean bean, HttpSession session) {
        CustomerEntity entity = YYlcBeanUtils.copyProperties(bean, new CustomerEntity());

        logger.info("updateCustomer: {}", entity);
        customerDao.update(entity.trim());
        logger.info("updateCustomer {} success", entity.getServiceName());

        session.setAttribute("user", customerDao.select(bean.getServiceId()));
        return 0;
    }

    public int deleteCustomer(CustomerBean bean) {
        CustomerEntity entity = YYlcBeanUtils.copyProperties(bean, new CustomerEntity());

        logger.info("deleteCustomer: {}", entity);
        if (bean.getStatus() != R.customerStatus.DELETE) {
            entity.setStatus(R.customerStatus.DELETE);
            customerDao.update(entity);
        }
        logger.info("deleteCustomer {} success", entity.getServiceName());

        return 0;
    }

}
